package com.eqdd.yiqidian.ui.gongneng.renshi.train

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.TrainRequestBean
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.eqdd.yiqidian.kotlin.bottomDialog
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.activity_recyclerview_refresh.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult
import java.util.*

/**
 * Created by jhj on 18-9-11.
 */
class TrainRequestListActivity : BaseActivity() {

    private var mvcHelper: MVCCoolHelper<List<TrainRequestBean>>? = null

    override fun initBinding() {
        setContentView(R.layout.activity_recyclerview_refresh)
        initTopTitleBar(View.VISIBLE, "培训需求列表")
        initTopRightImage(R.mipmap.addg) {
            bottomDialog(listOf("从培训申请选择添加", "线下已获得领导同意，自己添加")) {

                when (it) {
                    1 -> startActivityForResult<TrainRequestAddActivity>(1000, Config.TYPE to 1)
                    2 -> startActivityForResult<TrainRequestAddActivity>(1000, Config.TYPE to 0)
                }
            }
        }

    }


    override fun initData() {
        var pageNum = 0
        val list = arrayListOf<TrainRequestBean>()
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerLineItemDecoration(this))
        val slimAdapterEx = SlimAdapterEx.create()
                .register<TrainRequestBean>(R.layout.list_item_185, SlimInjector<TrainRequestBean> { data, injector ->
                    injector
                            ?.text(R.id.tv_item_title, data?.theTheme)
                            ?.clicked(R.id.tv_teacher) {
                                startActivity<TrainTeacherActivity>(Config.ID to data?.id)
                            }
                            ?.clicked(R.id.tv_course) {
                                startActivity<TrainCourseActivity>(Config.ID to data?.id)
                            }

                })
                .attachTo(recyclerView)
                .updateData(list as List<*>?) as SlimAdapterEx<TrainRequestBean>?
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener { recyclerView, position, v ->
                    val data = slimAdapterEx?.getDataItem(position)
                    startActivity<TrainRequestDetailActivity>(Config.ID to data?.id)
                }

        mvcHelper = MVCCoolHelper<List<TrainRequestBean>>(coolRefreshView)
        val dataSource = ModelRx2DataSource<TrainRequestBean>(ModelRx2DataSource.OnLoadSource<TrainRequestBean> { page, register ->
            if (page == 1) {
                pageNum = 0
            }
            OkGo.post<HttpResult<List<TrainRequestBean>>>(HttpConfig.BASE_URL + HttpConfig.TRAIN_REQUEST_LIST)
                    .params("comid", user.companyId)
                    .params("userGuid", user.guid)
                    .params("page", pageNum)
                    .converter(object : JsonConverter<HttpResult<List<TrainRequestBean>>>() {
                        override fun test() {
                            super.test()
                        }
                    })
                    .adapt(FlowableBody<HttpResult<List<TrainRequestBean>>>())
                    .flatMap({ result ->
                        if (page == 1) {
                            mvcHelper?.isAutoLoadMore = true
                        }
                        pageNum = result.nextpage
                        Flowable.just<List<TrainRequestBean>>(result.items ?: ArrayList())
                    })


        }, 10)

        mvcHelper?.setDataSource(dataSource)
        mvcHelper?.adapter = slimAdapterEx
        mvcHelper?.refresh()

    }

    override fun setView() {
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                1000 -> mvcHelper?.refresh()
            }
        }
    }
}